#!/bin/bash

function is_check_build_install_directory() {
  if [[ $# -ne 3 ]]; then
    echo "The format is as follows:"
    echo "bash $3 buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
  fi

  [[ ! "$1" =~ ^/.* || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
  [[ ! "$2" =~ ^/.* || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1
  echo "# ------------------------------------------------------------------------"
  echo "# $3 正在运行"
}

function is_check_build_equal() {
  local buildpath
  local installpath
  buildpath="$1"
  installpath="$2"
  [[ "${buildpath}" =~ .*/$ ]] && buildpath=${buildpath%/*}
  [[ "${installpath}" =~ .*/$ ]] && installpath=${installpath%/*}
  [[ "${buildpath}" == "${installpath}" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
  echo "# 构建路径为$1"
  echo "# 安装路径为$2"
}

function create_dir() {
  local path
  path="$1"
  [[ "$path" =~ .*/$ ]] && path=${path%/*}
  if [[ ! -d "${path}" && ! -f "${path}" ]]; then
    mkdir -p "${path}"
  elif [[ -n "$(ls -A "$1")" ]]; then
    path=$path$(date "+%y%m%d%H%M%S") && mkdir -p "${path}"
  fi
  echo "${path}"
}

function set_script_dir() {
  local REL_SCRIPT_DIR
  REL_SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")" || return 1
  SCRIPT_DIR="$(cd "${REL_SCRIPT_DIR}" && pwd)" || return 1
}
